package com.shishuo.cdn.util;

import java.io.IOException;
import java.net.URLDecoder;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.dotnet.common.constant.SystemConstants;

public class sendsms {

	private static String Url = "http://106.ihuyi.cn/webservice/sms.php?method=Submit";

	public static void main(String[] args) {
		SendSms("15300097795", "123456", "http://106.ihuyi.cn/webservice/sms.php?method=Submit");
	}

	public static String regSendSms(String telephone, String message, HttpServletRequest request) {
		System.out.println("进入注册短信发送方法");
		System.out.println("短信验证码当前状态为:"+request.getSession().getAttribute(SystemConstants.SMS_NAME));
		if(null!=request.getSession().getAttribute(SystemConstants.SMS_NAME)){
			if((boolean)request.getSession().getAttribute(SystemConstants.SMS_NAME)){
				return "4085";
			}
		}else{
			System.err.println("短信验证码当前session为null");
			return "4085";
		}
		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的验证码是：" + message + "。请不要把验证码泄露给其他人。");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 
	 * 描述：注册验证码
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-15 下午3:43:45
	 * @version
	 */
	public static String sendsms(String telephone, String message) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的验证码是：" + message + "。请不要把验证码泄露给其他人。");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 
	 * 描述：发送新密码
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-15 下午3:42:46
	 * @version
	 */
	public static String sendSmstoNewPwd(String telephone, String message) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的新密码为：" + message + "。请妥善保管，不要轻易泄露给其他人。");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 
	 * 描述：发送新支付密码
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-15 下午3:42:46
	 * @version
	 */
	public static String sendSmstoNewPayPwd(String telephone, String message) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的新支付密码为：" + message + "。请妥善保管，不要轻易泄露给其他人。");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 
	 * 描述：找回密码 发送校验码
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-15 下午3:42:46
	 * @version
	 */
	public static String sendSmsByBackPwd(String telephone, String message) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您找回密码的校验码为：" + message + "。请妥善保管，以防泄露。 ");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	/**
	 * 
	 * 描述：找回支付密码 发送校验码
	 * 创建人： 魏泽超
	 * 创建时间: 2016-7-15 下午3:42:46
	 * @version
	 */
	public static String sendSmsByBackPayPwd(String telephone, String message) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您找回支付密码的校验码为：" + message + "。请妥善保管，以防泄露。 ");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	public static String SendSms(String telephone, String message, String Url) {

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(Url);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的验证码是：" + message + "。请不要把验证码泄露给其他人。");
		String code = "";

		NameValuePair[] data = {// 提交短信
		new NameValuePair("account", "cf_wstl"), new NameValuePair("password", "7m5xV5"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("mobile", telephone), new NameValuePair("content", content), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			// System.out.println(SubmitResult);

			Document doc = DocumentHelper.parseText(SubmitResult);
			Element root = doc.getRootElement();

			code = root.elementText("code");
			String msg = root.elementText("msg");
			String smsid = root.elementText("smsid");

			System.out.println(code);
			System.out.println(msg);
			System.out.println(smsid);

			if ("4085".equals(code) && "0".equals(smsid)) {
				return code;
			}

			if ("2".equals(code)) {
				// System.out.println("短信提交成功");
				return code;
			}

		} catch (HttpException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return code;
	}

	private final static String BAIFEN_URL = "http://211.99.213.49:8055/report";

	/**
	 * 百分通联 短信发送 2015年9月6日 09:58:51 YangJ
	 */
	public static boolean baifen_sendsms(String telephoneNumber, String message) {

		// $target = "http://211.99.213.49:8055/report";

		// $post_data ="loginname=zhonghuahun&password=01044110&spnumber=06454&msg=".rawurlencode($message)."&mobile=".$telephone."&gatename=wangxun";

		HttpClient client = new HttpClient();
		PostMethod method = new PostMethod(BAIFEN_URL);

		// client.getParams().setContentCharset("GBK");
		client.getParams().setContentCharset("UTF-8");
		method.setRequestHeader("ContentType", "application/x-www-form-urlencoded;charset=UTF-8");

		// int mobile_code = (int)((Math.random()*9+1)*100000);
		// System.out.println(mobile);

		String content = new String("您的验证码是：" + message + "。请不要把验证码泄露给其他人。");

		NameValuePair[] data = {// 提交短信
		new NameValuePair("loginname", "zhonghuahun"), new NameValuePair("password", "01044110"), // 密码可以使用明文密码或使用32位MD5加密
				// new NameValuePair("password", util.StringUtil.MD5Encode("密码")),
				new NameValuePair("spnumber", "06454"), new NameValuePair("msg", content), new NameValuePair("mobile", telephoneNumber), new NameValuePair("gatename", "wangxun"), };

		method.setRequestBody(data);

		try {
			client.executeMethod(method);

			String SubmitResult = method.getResponseBodyAsString();

			Document doc = DocumentHelper.parseText(SubmitResult);
			String result = doc.getRootElement().elementText("result");
			String resultmsg = URLDecoder.decode(doc.getRootElement().elementText("resultmsg"));
			System.out.println("----百分短息接口 返回：" + result + " " + resultmsg);
			// System.out.println("----百分短息接口 返回："+SubmitResult);
			// System.out.println("----百分短息接口 返回："+doc);

			// return isset( $callback_arr['resultcode']['result']) ? (0==$callback_arr['resultcode']['result'] ? true : false) : false;
			if ("0".equals(result))
				return true;
			else
				return false;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;
	}

}